﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using BOMS.DBUtility;//请先添加引用
namespace BOMS.DAL
{
    /// <summary>
    /// 数据访问类S_BatgrpSubLog。
    /// </summary>
    public class S_BatgrpSubLog
    {
        public S_BatgrpSubLog()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BOMS.Model.S_BatgrpSubLog model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into S_BatgrpSubLog(");
            strSql.Append("Generation,EquipID,BatteryGroup,Vval,Ival,CdFree,CdTime,CapFree,TimeFree,WorkStatus,WarningContent,Status,WarningContentDescr,LogTime)");
            strSql.Append(" values (");
            strSql.Append("@Generation,@EquipID,@BatteryGroup,@Vval,@Ival,@CdFree,@CdTime,@CapFree,@TimeFree,@WorkStatus,@WarningContent,@Status,@WarningContentDescr,@LogTime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Generation", SqlDbType.TinyInt,1),
					new SqlParameter("@EquipID", SqlDbType.NVarChar,50),
					new SqlParameter("@BatteryGroup", SqlDbType.NVarChar,50),
					new SqlParameter("@Vval", SqlDbType.Decimal,9),
					new SqlParameter("@Ival", SqlDbType.Decimal,9),
					new SqlParameter("@CdFree", SqlDbType.Decimal,9),
                    new SqlParameter("@CdTime", SqlDbType.NVarChar,50),
                    new SqlParameter("@CapFree", SqlDbType.Decimal,9),
                    new SqlParameter("@TimeFree", SqlDbType.NVarChar,50),
                    new SqlParameter("@WorkStatus", SqlDbType.TinyInt),
                    new SqlParameter("@WarningContent", SqlDbType.TinyInt),
                    new SqlParameter("@Status", SqlDbType.NVarChar,30),
					new SqlParameter("@WarningContentDescr", SqlDbType.NVarChar,30),
					new SqlParameter("@LogTime", SqlDbType.DateTime)};
            parameters[0].Value = model.Generation;
            parameters[1].Value = model.EquipID;
            parameters[2].Value = model.BatteryGroup;
            parameters[3].Value = model.Vval;
            parameters[4].Value = model.Ival;
            parameters[5].Value = model.CdFree;
            parameters[6].Value = model.CdTime;
            parameters[7].Value = model.CapFree;
            parameters[8].Value = model.TimeFree;
            parameters[9].Value = model.WorkStatus;
            parameters[10].Value = model.WarningContent;
            parameters[11].Value = model.Status;
            parameters[12].Value = model.WarningContentDescr;
            parameters[13].Value = model.LogTime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 1;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BOMS.Model.S_BatgrpSubLog GetModel(long LogID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 LogID,Generation,EquipID,BatteryGroup,Vval,Ival,CdFree,CdTime,CapFree,TimeFree,WorkStatus,WarningContent,Status,WarningContentDescr,LogTime from S_BatgrpSubLog ");
            strSql.Append(" where LogID=@LogID ");
            SqlParameter[] parameters = {
					new SqlParameter("@LogID", SqlDbType.BigInt)};
            parameters[0].Value = LogID;

            BOMS.Model.S_BatgrpSubLog model = new BOMS.Model.S_BatgrpSubLog();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                AssignModel(ds.Tables[0].Rows[0], model);
                return model;
            }
            else
            {
                return null;
            }
        }
        public void AssignModel(DataRow dr, Model.S_BatgrpSubLog model)
        {
            if (dr.Table.Columns.Contains("LogID") && dr["LogID"].ToString() != "")
            {
                model.LogID = (long)dr["LogID"];
            }
            if (dr["Generation"].ToString() != "")
            {
                model.Generation = int.Parse(dr["Generation"].ToString());
            }
            model.EquipID = dr["EquipID"].ToString();
            model.BatteryGroup = dr["BatteryGroup"].ToString();
            if (dr["Vval"].ToString() != "")
            {
                model.Vval = decimal.Parse(dr["Vval"].ToString());
            }
            if (dr["Ival"].ToString() != "")
            {
                model.Ival = decimal.Parse(dr["Ival"].ToString());
            }
            if (dr["CdFree"].ToString() != "")
            {
                model.CdFree = decimal.Parse(dr["CdFree"].ToString());
            }
            if (dr["CdTime"].ToString() != "")
            {
                model.TimeFree = dr["CdTime"].ToString();
            }
            if (dr["CapFree"].ToString() != "")
            {
                model.CapFree = decimal.Parse(dr["CapFree"].ToString());
            }
            if (dr["TimeFree"].ToString() != "")
            {
                model.TimeFree = dr["TimeFree"].ToString();
            }
            if (dr["WorkStatus"].ToString() != "")
            {
                model.WorkStatus = byte.Parse(dr["WorkStatus"].ToString());
            }
            if (dr["WarningContent"].ToString() != "")
            {
                model.WarningContent = int.Parse(dr["WarningContent"].ToString());
            }
            model.Status = dr["Status"].ToString();
            model.WarningContentDescr = dr["WarningContentDescr"].ToString();
            if (dr["LogTime"].ToString() != "")
            {
                model.LogTime = DateTime.Parse(dr["LogTime"].ToString());
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LogID,Generation,EquipID,BatteryGroup,Vval,Ival,CdFree,CdTime,CapFree,TimeFree,WorkStatus,WarningContent,Status,WarningContentDescr,LogTime");
            strSql.Append(" FROM S_BatgrpSubLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" LogID,Generation,EquipID,BatteryGroup,Vval,Ival,CdFree,CdTime,CapFree,TimeFree,WorkStatus,WarningContent,Status,WarningContentDescr,LogTime ");
            strSql.Append(" FROM S_BatgrpSubLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetListFromPage(string strWhere, string strOrderby, int pageSize, int curPage, out int recordCount)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select LogID,Generation,EquipID,BatteryGroup,Vval,Ival,CdFree,CdTime,CapFree,TimeFree,WorkStatus,WarningContent,Status,WarningContentDescr,LogTime ");
            strSql.Append(" FROM S_BatgrpSubLog ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DALUti.GetPageData(strSql.ToString(), strOrderby, pageSize, curPage, out recordCount);
        }

        #endregion  成员方法
    }
}

